package com.company.cloud.web.render;

import java.util.HashMap;
import java.util.Map;

public enum ContentType {


    TEXT("text/plain"),
    HTML("text/html"),
    XML("text/xml"),
    JSON("application/json"),
    JAVASCRIPT("application/javascript");

    private final String value;

    private ContentType(String value) {
        this.value = value;
    }

    public String value() {
        return value;
    }

    public String toString() {
        return value;
    }

    // ----------------------------------------------------

    private static final Map<String, ContentType> mapping = initMapping();

    /**
     * 将简写的文本射到 context type，方便在 Controller.renderText(String, String)
     * 之中取用，例如：
     *   renderText(..., "xml")
     *   比下面的用法要省代码
     *   renderText(..., "text/xml")
     */
    private static Map<String, ContentType> initMapping() {
        Map<String, ContentType> ret = new HashMap<>();
        ret.put("text", TEXT);
        ret.put("plain", TEXT);
        ret.put("html", HTML);
        ret.put("xml", XML);
        ret.put("json", JSON);
        ret.put("javascript", JAVASCRIPT);
        ret.put("js", JAVASCRIPT);
        ret.put("TEXT", TEXT);
        ret.put("PLAIN", TEXT);
        ret.put("HTML", HTML);
        ret.put("XML", XML);
        ret.put("JSON", JSON);
        ret.put("JAVASCRIPT", JAVASCRIPT);
        ret.put("JS", JAVASCRIPT);
        return ret;
    }

    public static ContentType parse(String str) {
        return mapping.get(str);
    }
}
